package com.sunk.springboot.bolg.springbootblog.controller;

import com.sunk.springboot.bolg.springbootblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class BlogController {
    @Autowired
    private ArticleService service;
    @RequestMapping(value = "articleList")
    public String articleList(HttpServletRequest req, HttpServletResponse resp, Integer pageNum, ModelMap modelMap) {
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageNum < 0) {
            pageNum = 0;
        }
        Sort sort = Sort.by(Sort.Direction.DESC, "modified");
        PageRequest pageRequest = PageRequest.of(pageNum, 2, sort);
        modelMap.addAttribute("articleList", service.listUser(pageRequest));
        return "/client/index";
    }
}
